
package com.dps.omc.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.utils.ErrorAnalyzer;
import com.dps.omc.model.ErrorKeyWord;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.omc.service.IErrorKeyWordService;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class ErrorKeyWordAction extends AbstractAdminController<IErrorKeyWordService>{

    @Autowired
    private IErrorKeyWordService errorKeyWordService;
    @Autowired
    private ErrorAnalyzer errorAnalyzer;
    
    @RequestMapping(value="/errorkeyword/list")
    public String list(ErrorKeyWord errorKeyWord, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(errorKeyWord); 	
    	BizData4Page<ErrorKeyWord> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("errorKeyWord", errorKeyWord);
    	return "/module/errorkeyword/list";
    }    
    
    @RequestMapping(value="/errorkeyword/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("errorKeyWord", new ErrorKeyWord());
        return "module/errorkeyword/edit";
    }    
    
    @RequestMapping(value="/errorkeyword/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	ErrorKeyWord errorKeyWord = errorKeyWordService.fetch(id);
    	model.addAttribute("errorKeyWord", errorKeyWord);
        return "module/errorkeyword/edit";
    }    
    
    @RequestMapping(value="/errorkeyword/save")
    @ResponseBody
    public String save(HttpServletRequest request,ErrorKeyWord errorKeyWord){
		boolean isAddNew = StringUtils.isBlank(errorKeyWord.getId())?true:false;
		if(isAddNew)
		{
			errorKeyWord.setId(IdGenerator.createNewId());
			errorKeyWordService.add(errorKeyWord);
		}
		else
		{
			errorKeyWordService.update(errorKeyWord);
		}
        return JsonResult.operateSuccessClose(Constants.MAIN_TAB,"日志错误关键字已修改,但未生效，请重载系统参数！").toString();
//        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/errorkeyword/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	errorKeyWordService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }

    @RequestMapping(value="/errorkeyword/reload")
    @ResponseBody
    public String reload(String ids,HttpServletRequest request){

        errorAnalyzer.reload();
        return JsonResult.operateSuccess(Constants.MAIN_TAB).toString();
    }
	
    protected Map getQueryMap(ErrorKeyWord errorKeyWord)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("keyWord", errorKeyWord.getKeyWord());		
		conditions.put("createDate", errorKeyWord.getCreateDate());		
		conditions.put("createrName", errorKeyWord.getCreaterName());		
		conditions.put("createrId", errorKeyWord.getCreaterId());		
    	return conditions;
    }

    @Override
    protected IErrorKeyWordService getMainService() {
        return errorKeyWordService;
    }

    @Override
    protected String getMainObjName() {
        return "errorkeyword";
    }

    @Override
    protected String getViewTitle() {
        return "errorkeyword";
    }
}
